<?php

class BDMySQLCreateClass{
	
	var $conn;
	var $resource;
	
	public function  __construct($bd_conf, $bd_schema){
		$this->conn = new BDMySQLConnect($bd_conf);
		$this->conn->select_data_base("bd_teste");
		$this->resource = new XMLClass($bd_schema);
		$this->resource = $this->resource->getResourceFile();
		$this->generate();
	}
	
	private function generate(){
		foreach($this->resource as $table){
			if(!$this->tableExists($table['name']))
				$this->CreateTable($table);
		}
	}
	
	public function tableExists($tableName){
		$tables = mysql_list_tables($this->conn->getDatabase(), $this->conn->conn);
		$num_rows = mysql_num_rows($tables);
		for ($i = 0; $i < $num_rows; $i++) {
    		if(mysql_tablename($tables, $i) === $tableName)
    			return true;
		}
		return false;
	}
	
	private function CreateTable($table){
		$query = "CREATE TABLE {$table['name']} (\n";
		foreach($table->column as $column){
			$query .= $column['name']." ";
			$query .= $column['type'];
			if($column->option){
				foreach($column->option as $item){
					$query .= " ".$item." ";
				}
			}
			$query .= ",\n";
		}
		$query = substr($query, 0, -2);
		$query .= ")";
		echo $query."\n";
	} 
	
}

?>